<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../includes/init.php');
require ('./configs/site_config.php');
require('./classes/control/control_bar.php');
require('./classes/control/control_template.php');
class bar
{

	var $bar_c;
	var $template_c;
	var $webType;//网站类型
	/**
	* @ 构造函数,实例化barControl
	* @ __construct
	*/

	public function __construct()
	{
		$this->bar_c = new barControl();
		$this->template_c = new TemplateControl();
		$this->webType = empty($_REQUEST['web_type'])?1:(int)trim($_REQUEST['web_type']);
	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index()
	{
		$url_info = $bar_info = array();
		$page_size = 20;//设置每页显示20条
		$block_name	= !empty($_POST['block_name']) ? $_POST['block_name'] : urldecode($_GET['block_name']);
		
		$block_title	= !empty($_POST['block_title']) ? $_POST['block_title'] : urldecode($_GET['block_title']);
		$block_name  = $block_name == '输入模块名称'?'':$block_name;
		$block_title  = $block_title == '输入显示标题'?'':$block_title;
		
		$page = empty($_REQUEST['page'])? 1:(int)$_REQUEST['page'];
		if($_POST['search']||!empty($_REQUEST['url_page']))
		{
			$url_info = $this->bar_c->get_url_info($page,'list_s',$this->webType,$block_name,$block_title);
			$bar_info = $this->bar_c->get_bar_info('list_s',$url_info['page_info']['start'],$page_size,$this->webType
			,$block_name,$block_title);
		}
		else
		{
			$url_info = $this->bar_c->get_url_info($page,'list',$this->webType);
			$bar_info = $this->bar_c->get_bar_info('list',$url_info['page_info']['start'],$page_size,$this->webType);
		}
		$GLOBALS['smarty']->assign('web_info',   get_web_info($this->webType));
		$GLOBALS['smarty']->assign('web_type',     $this->webType);
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('bar_info',$bar_info );
		$GLOBALS['smarty']->assign('block_name',stripslashes($block_name));
		$GLOBALS['smarty']->assign('block_title',stripslashes($block_title));
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('bar_index.htm'); 
	
	}
	
	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	public function add_edit()
	{	
		if($_REQUEST['act'] == 'add')
		{
			if($_POST['sub_from'])
			{ 
				$affect = $this->bar_c->operate_bar('add',0,trim($_POST['block_name']),trim($_POST['block_title']),
				trim($_POST['block_pic_url']),trim($_POST['block_pic']),trim($_POST['block_more']),
				trim($_POST['block_content']),$this->webType);
				if($affect == 1)
				{
					echo  '<script type="text/javascript"> 
					alert("增加成功！");window.location.href="/wondershare/bar.php?action=index&web_type='.$this->webType.'";</script>';
					exit;
				}
			}
			$GLOBALS['smarty']->assign('web_info',   get_web_info($this->webType));
			$GLOBALS['smarty']->assign('web_type',     $this->webType);
			$GLOBALS['smarty']->assign('tile',     '增加');
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->display('bar_info.htm');
		}
		elseif($_REQUEST['act'] == 'edit')
		{
			$bar_info = array();
			if($_POST['sub_from'])
			{
				if(!empty($_POST['block_id']))
				{
					//删除队列
					queueDelete(0, $_POST['block_id']);
					
					$affect = $this->bar_c->operate_bar('edit',(int)$_POST['block_id'],($_POST['block_name']),($_POST['block_title']),
					($_POST['block_pic_url']),($_POST['block_pic']),($_POST['block_more']),
					($_POST['block_content']));
					if($affect == 1)
					{
						echo  '<script type="text/javascript"> alert("编辑成功！"); </script>';
						
						$this->updateTemplate((int) $_POST['block_id']);
					}
				}
			}else {
				//加入队列
				queueInfo($_GET['block_id']);
			}
			$bar_info = $this->bar_c->get_bar_id_info((int)$_GET['block_id']);
			$GLOBALS['smarty']->assign('web_info',   get_web_info($this->webType));
			$GLOBALS['smarty']->assign('web_type',     $this->webType);
			$GLOBALS['smarty']->assign('tile',     '修改');
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('bar_info',$bar_info[0] );
			$GLOBALS['smarty']->display('bar_info.htm'); 	
		}
	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	public function del()
	{
		if(!empty($_GET['checkList']))
		{
			$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
			$this->bar_c->delete_bar($checkList);
		}
	}
	
	/**
	* 修改与发布模板操作
	* @param   int      $block_id  产品属性ID
	* @param   int      $w_type   网站标识
	
	* @return  string
	*/
	function updateTemplate($block_id = 0) {
		
		//定义更新分类模板
		$update_arr = array('id' => 0,
							'w_type' => (int) $this->webType,
							'operation_type' => 'product',
							'type' => 'update',
							'location' => '/wondershare/product_attributes.php?action=index&web_type='.$this->webType,				
		);
		
		$sql = "SELECT id FROM won_product 
				WHERE FIND_IN_SET('{$block_id}', won_product_guide_bar) AND won_product_delete = 0 AND web_type = '{$this->webType}' ";
	
		$result = $GLOBALS['db']->query($sql);
	
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			$update_arr['id'] = $row['id'];
		    $this->template_c->updateTemplate($update_arr);
		}
		
		echo '<script type="text/javascript">
				window.location.href="/wondershare/bar.php?action=index&web_type='.$this->webType.'";		
			  </script>';
		
	}
}
$action    = $_REQUEST['action']; 
$bar   = new bar;
if(method_exists($bar, $action)) 
{
	$bar->$action();
}else{
	$bar->index();
}
	
?>
